package com.tanhua.server.controller;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.MomentVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.server.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 评论管理控制层
 */
@RestController
@RequestMapping("/comments")
public class CommentController {

    @Autowired
    private CommentService commentService;

    /**
     * 分页查询评论列表
     * movementId:动态id
     */
    @RequestMapping(method = RequestMethod.GET)
    public ResponseEntity queryPageByPublishId(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int pagesize,String movementId) {
        PageResult<CommentVo> pageResult = commentService.queryPageByPublishId(page, pagesize,movementId);
        return ResponseEntity.ok(pageResult);
    }



    /**
     * 发布评论
     */
    @RequestMapping(method = RequestMethod.POST)
    public ResponseEntity saveComment(@RequestBody Map<String,String> params) {
        String publishId = params.get("movementId");//发布动态id
        String content = params.get("comment");//针对动态评论内容
        commentService.saveComment(publishId,content);
        return ResponseEntity.ok(null);
    }


    /**
     * 评论点赞
     * commentId:评论id(被评论的主键id)
     */
    @RequestMapping(value = "/{id}/like",method = RequestMethod.GET)
    public ResponseEntity saveCommentLike(@PathVariable("id") String commentId) {
        int likeCount = commentService.saveCommentLike(commentId);
        return ResponseEntity.ok(likeCount);
    }


    /**
     * 评论取消点赞
     * commentId:评论id(被评论的主键id)
     */
    @RequestMapping(value = "/{id}/dislike",method = RequestMethod.GET)
    public ResponseEntity saveCommentDisLike(@PathVariable("id") String commentId) {
        int likeCount = commentService.saveCommentDisLike(commentId);
        return ResponseEntity.ok(likeCount);
    }

}
